const express = require('express')
const app = express()
app.use(express.static('public'))
//解决跨域
const cors = require('cors')
app.use(cors())

//上传图片
const fs = require('fs');
const multer  = require('multer')

const upload = multer({ dest: 'public/uploads/' });

// 单图上传
app.post('/upload', upload.single('file'), function(req, res, next){
    const file = req.file
    file.url=`http://localhost:5000/uploads/${file.filename}`
    res.send({
        code: 200,
        result:file,
        msg: '上传成功'
    })
    // res.send({ret_code: '0'});
});
//引包
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())


// app.use(express.static(path.join(__dirname, 'public')))

require('./router/index')(app)
require('./db/mongoose')(app)


app.listen(5000,(req,res)=>{
    console.log('服务器启动了')
})
